﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CardLogistics.Model.Sys;

namespace CardLogistics.Web.Components
{
    /// <summary>
    /// 用户登录相关。
    /// </summary>
    public class AuthCookie
    {
        HttpContext context;
        const string cookieName = "jiazhuoxxkj";

        /// <summary>
        /// 构造函数
        /// </summary>
        public AuthCookie()
        {
            context = HttpContext.Current;
        }
        /// <summary>
        /// 用户是否已经过认证
        /// </summary>
        internal bool IsAuthenticated
        {
            get
            {
                HttpCookie cookie = context.Request.Cookies[cookieName];
                return cookie != null;
            }
        }

        /// <summary>
        /// 将登录成功的用户信息保存在Cookie中
        /// </summary>
        /// <param name="user">用户SysUser对象</param>
        internal void SetLoggedUser(Sys_SysUserInfo user)
        {

            HttpCookie cookie = new HttpCookie(cookieName);
            cookie.Value = System.Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(user.ID));
            context.Response.Cookies.Add(cookie);
            context.Session[cookieName] = user;
        }

        /// <summary>
        /// 获取登录的用户对象
        /// </summary>
        /// <returns></returns>
        internal Sys_SysUserInfo GetLoggedUser()
        {
            try
            {
                Sys_SysUserInfo user = (Sys_SysUserInfo)context.Session[cookieName];
                //如果Session中保存的用户信息为空，则检查登录验证Cookie，
                //若该Cookie存在，则根据Cookie中保存的用户编号来创建一个新的用户对象并将其保存在Session中
                //若Cookie不存在，则返回null，迫使用户重新登录
                if (user == null)
                {
                    string cookieValue = context.Request.Cookies[cookieName].Value;
                    if (cookieValue == null)
                    {
                        return null;
                    }
                    string idStr = System.Text.Encoding.Default.GetString(System.Convert.FromBase64String(cookieValue));
                    if (idStr != null)
                    {
                        try
                        {
                            user = new CardLogistics.DAL.Sys.Sys_SysUserInfoData().GetModel(idStr);
                            context.Session[cookieName] = user;
                            return user;
                        }
                        catch
                        {
                            //出现意外，可能用户私自修改了Cookie的值
                            //使Cookie过期而被清除，迫使用户重新登录
                            HttpCookie cookie = context.Request.Cookies[cookieName];
                            cookie.Value = null;
                            cookie.Expires = DateTime.Today.AddYears(-8);
                            context.Response.Cookies.Add(cookie);
                            //同时清除所有Session对象
                            context.Session.Abandon();
                            return null;
                        }
                    }
                    else
                        return null;
                }
                return user;
            }
            catch
            {
                return null;
            }

        }

        /// <summary>
        /// 清空保存当前登录的用户信息的Session对象，以便重新从数据库加载该用户数据
        /// </summary>
        internal void UpdateUserInfo()
        {
            context.Session[cookieName] = null;
        }

        /// <summary>
        /// 用户退出登录
        /// </summary>
        internal void Logout()
        {
            HttpCookie cookie = context.Request.Cookies[cookieName];
            cookie.Value = null;
            cookie.Expires = DateTime.Today.AddYears(-8);
            context.Response.Cookies.Add(cookie);
            context.Session.Abandon();
        }
    }
}
